package com.talking.view;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.talking.App;
import com.talking.R;
import com.talking.adapter.InvitationAdapter;
import com.talking.bean.InvitationBean;
import com.talking.databinding.ActivityInvitationBinding;
import com.talking.presenter.FriendPresenter;
import com.talking.presenter.HistoryPresenter;
import com.talking.utils.Utils;

import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class InvitationActivity extends AppCompatActivity {
    private ActivityInvitationBinding binding;
    private InvitationAdapter adp;
    private final List<InvitationBean> data=new ArrayList<>();
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (android.R.id.home==item.getItemId()){finish();}
        if (item.getItemId()== R.id.menu_invitate){
            JSONArray ja=new JSONArray();
            for (InvitationBean bean :data) {
                if (bean.isChoosed){
                    ja.put(bean.friendAccount);
                }
            }
            HistoryPresenter.sendGroupInvitation(App.sp.getString("account", ""),ja,App.groupBean.id, new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    e.printStackTrace();
                }
                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) {
                    runOnUiThread(() -> {
                        Utils.toast( "已发送邀请");
                        finish();
                    });
                }
            });
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_invitate, menu);
        return true;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding=ActivityInvitationBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        adp=new InvitationAdapter(this,data);
        binding.invitationList.setAdapter(adp);
        binding.invitationList.setOnItemClickListener((parent, view, position, id) -> {
            CheckBox cb = view.findViewById(R.id.item_invitation_cb);
            data.get(position).isChoosed= !cb.isChecked();
            cb.setChecked(!cb.isChecked());
        });
        FriendPresenter.getFriendsWhoNotInGroup(App.sp.getString("account", ""), App.groupBean.id, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();
            }
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) {
                runOnUiThread(() -> {
                    data.clear();
                    try {
                        JSONArray ja = new JSONObject(response.body().string()).getJSONArray("data");
                        for (int i = 0; i < ja.length(); i++) {
                            JSONObject jo = ja.getJSONObject(i);
                            data.add(new InvitationBean(jo.getString("remark"),jo.getString("friendAccount"),jo.getString("createTime"),false));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    adp.notifyDataSetChanged();
                });
            }
        });
    }
}